package com.atiguigu.rabbitmq.six;

import com.atiguigu.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ReceiverLogsDirect01 {
    public static final String EXCHANGE_NAME = "direct_logs";
    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMqUtils.getChannel();
        //声明交换机名、Direct类型
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        //声明一个队列

        channel.queueDeclare("console",false,false,false,null);
        /*
         * 绑定交换机和队列,通过第3个参数routingKey连接，参数1：队列和参数2：交换机
         * */
        channel.queueBind("console",EXCHANGE_NAME,"info");
        channel.queueBind("console",EXCHANGE_NAME,"warning");
        System.out.println("等待接收消息，接收到消息打印在屏幕");
        DeliverCallback deliverCallback = (consumerTag, message)->{
            System.out.println("ReceiverLogsDirect01接收消息:"+new String(message.getBody()));
        };
        CancelCallback callback= consumerTag->{
            System.out.println(consumerTag+"消费者取消消费接口回调逻辑");
        };
        channel.basicConsume("console",true,deliverCallback,callback);
    }
}
